package functional;

interface Strategy{
    String approach(String msg);
}
class Soft implements Strategy {
    public String approach(String msg){
        return msg.toLowerCase()+"?";
    }
}

class Unrelated{
    static String twice(String msg){
        return msg+" "+msg;
    }
}

public class Strategize{
    Strategy strategy;
    String msg;
    Strategize(String msg){
        strategy = new Soft();
        this.msg = msg;
    }

    void communicate(){
        System.out.println(strategy.approach(msg));
    }

    void changeStrategy(Strategy strategy){
        this.strategy = strategy;
    }

    public  static  void main(String[] args){
        Strategy[]  strategies={
                new Strategy() {
                    public String approach(String msg){
                        return msg.toUpperCase()+"!";
                    }
                },//内部类
                msg->msg.substring(0,5),//lambda表达式
                Unrelated::twice//方法引用
        };

        Strategize s = new Strategize("Hello there");
        s.communicate();

        for(Strategy newStrategy:strategies){
            s.changeStrategy(newStrategy);
            s.communicate();
        }
    }
}

